<?php

/**
 *  用户中心控制器
 */
class UserInfoController extends  CommonController {
	
	private $model;
	public function __init(){
		parent::__init();
		$this->model = K('MyData');
		if(session('?uid')===FALSE || session('?username')===FALSE){
			$this->success('您还没有登陆呢!',U('Login/index'));
		}
		
	}
	
	/**
	 * 个人中心首页
	 */
	public function index() {
		//显示当前帐户余额
		$uid = session('uid');
		//可用余额
		$this->assign('usermoney',K('Users')->where("uid={$uid}")->getField('user_money'));
		//总消息记录
		$consume = current(K('GoodsOrder')->field('sum(count_price) as count')->where("(users_uid={$uid} AND order_status='已付款') OR (users_uid={$uid} AND order_status='交易完成')")->all());
		$consume = !$consume['count']?$consume=0.00:$consume['count'];
		//获得当前用户的头像
		$face = K('Users')->where("uid={$uid}")->getField('face');
		$this->assign('face',$face);
		//进行中的订单 除了取消的订单和交易完成的
		$keepBus = current(K('GoodsOrder')->field('count(*) as c')->where("(users_uid={$uid} AND order_status!='交易完成') AND (users_uid={$uid} AND order_status!='取消订单')")->all());
		$this->assign('keepBus',$keepBus['c']);
		$this->assign('consume',$consume);
		
		$this -> display();
	}
	
	/**
	 * ajax返回相应状态的订单
	 */
	 public function ajaxStatusOrder(){
		//查询所有相应的订单
		if(!IS_AJAX) return $this->error('非法请求');
		$uid = session('uid');
		
		//定单模型
		$orderModel = K('GoodsOrder');
		//定单列表模型
		$orderList = K('OrderList');
		//商品模型
		$goodsModel = K('Goods');
		//当前用户的定单
		$userOrder = $orderModel->getUserOrderList();
		foreach($userOrder as $k => $order){
			$userOrder[$k]['goods'] = $orderList->getOrderList($order['oid']);
		}
//		p($userOrder);
		$this->assign('userOrder',$userOrder);
		$this->ajax($this->display(null,-1,null,'text/html',false));
	 }



	/**
	 * 订单中心
	 */
	public function orderList() {
		//查询所有相应的订单
		$uid = session('uid');
		//定单模型
		$orderModel = K('GoodsOrder');
		//定单列表模型
		$orderList = K('OrderList');
		//商品模型
		$goodsModel = K('Goods');
		//当前用户的定单
		$userOrder = $orderModel->getUserOrderList();
		foreach($userOrder as $k => $order){
			$userOrder[$k]['goods'] = $orderList->getOrderList($order['oid']);
			$userOrder[$k]['goods_gid'] = $userOrder[$k]['goods'][$k]['gid']; 
		}
		$this->assign('userOrder',$userOrder);
		$this -> display();
	}
		
	/**
	 * 订单明细
	 */
	 public function getOrderDetails(){
		 	$sn = Q('get.sn','','strval');
			$orderDetails = K('GoodsOrder')->orderDetails($sn);
//			p($orderDetails);
			$this->assign('orderDetails',$orderDetails);
			$this->display();
	 }
	
	
	
	/**
	 * 修改密码
	 */
	public function changePwd() {
		if(IS_POST){
			if(!$this->model->changePwd()){
				$this->error($this->model->error);
			}else{
				session(null);
				$this->success('您已经修改了密码,请重新登陆',U('Login/Index'));
			}
			
			
		}
		$this -> display();
	}
	
	/**
	 * 显示验证码
	 */
	 public function code(){
	 	$code = new Code();
		$code->show();
	 }
	
	/**
	 * ajax检测用户的密码是否输入正确
	 */
	 public function ajaxCheckPwd(){
	 	//获得到旧密码
	 	if(IS_AJAX){
	 		$oldPwd = Q('post.oldpassword');
			$username = md5(session('username'));
			// 返回相应的用户信息
			$userInfo = $this->model->findUserInfo($oldPwd,$username);
			//如果密码输入正确那么返回相应的用户信息
			if($userInfo){
				$this->ajax(array('message'=>'密码正确','status'=>1));
			}else{
				$this->ajax(array('message'=>'旧密码不正确','status'=>0));
			}
	 	}
	 }
	 
	 /**
	  * ajax检测验证码
	  */
	  public function ajaxCheckCode(){
	  	if(IS_AJAX){
	  		$pcode = strtoupper(Q('post.code'));
			if($pcode != session('code')){
				$this->ajax(array('message'=>'验证码输入有误','status'=>0));
			}else{
				$this->ajax(array('message'=>'验证码输入正确','status'=>1));
			}
	  	}
	  }

	
	/**
	 * 收货地址
	 */
	public function takeAddr() {
		//返回所有的收货地址
		$addrs = K('ReceiveAddr')->getUserRa(session('uid'));
		$this->assign('addrs',$addrs);
		$this->assign('prov',K('City')->getAllProvince());
		$this -> display();
	}
	
	/**
	 * 异步上传头像
	 */
	 public function uploadFace() {
		$upload = new Upload('Upload/face/' . date('y/m'));
		$data = current($upload -> upload());
		if (empty($data)) {
			$this -> ajax('上传失败');
		} else {
			//来这个地方可以缩略图处理
			$img = new Image();

			//缩略为230的
			$bigPath = $data['dir'] . $data['filename'] .'.'. $data['ext'];

			//230图
			$file['face'] = $img -> thumb($data['path'], $bigPath, 300, 300, 6);
			//删除原图
			$this -> ajax($file);
		}
	}
	
	
	/**
	 * 更新我的资料的方法
	 */
	 public function updateMyData(){
	 	
	 	if(IS_POST){
	 		//判断是否更新头像如果更新头像那么删除原来旧头像
	 		if(Q('post.oldface')!=Q('post.face')){
	 			unlink(Q('post.oldface'));
	 		}
	 		if($this->model->updateUserInfo()){
	 			$this->success('资料修改成功');
	 		}else{
	 			$this->error($this->model->error);
	 		}
	 	}
	 }
	 	
		/**
		 * 删除订单
		 */
		 public function delOrder(){
		 	if(!IS_AJAX) $this->error('非法请求');
			$oid = Q('post.oid',0,'intval');
			if(K('GoodsOrder')->delete($oid) && K('OrderList')->where("order_oid={$oid}")->delete()){
				$this->ajax(array('message'=>'删除成功','status'=>1));
			}else{
				$this->ajax(array('message'=>'删除失败','status'=>0));
			}
		 }
		
		
		/**
		 * ajax修改订单状态
		 */
		 public function cancelOrder(){
		 	
		 	if(!IS_AJAX) $this->error('非法请求');
			if(K('GoodsOrder')->update(Q('post.'))){
				$this->ajax(array('message'=>'订单已取消','status'=>1));
			}else{
				$this->ajax(array('message'=>'订单状态修改失败','status'=>0));
			}
		 }
	 
	 
	 
	 /**
	  * ajax检测用户昵称是否存在
	  */
	  public function ajaxCheckNickName(){
	  	 $nickname = Q('post.nickname');
		 if($this->model->where("nickname='{$nickname}'")->find()){
		 	$this->ajax(array('message'=>'昵称已经存在请更换','status'=>0));
		 }else{
		 	$this->ajax(array('message'=>'昵称可以使用','status'=>1));
		 }
	  }
	
	
	/**
	 * 我的资料
	 */
	public function myData() {
		$uid = session('uid');
		//用户信息
		$userInfo  =$this->model->getMyData($uid);
		$this -> assign('userInfo',$userInfo);
		//当前用户所有地区
		$this -> assign('area',explode(',',$userInfo['addarea']));
		$this -> threeArea($userInfo['addarea']);
		$this -> display();
	}
	
	/**
	 * 帐户余额
	 */
	 public function balance(){
	 	$uid = session('uid');
		//查询当前帐户的余额
	 	$this->assign('balance',$this->model->where("uid={$uid}")->getField('user_money'));
		//查询当前帐户的消费记录
		$business = K('GoodsOrder')->where("users_uid={$uid} AND order_status='已付款' || users_uid={$uid} AND order_status='交易完成'")->all();
		$this->assign('business',$business);
	 	$this->display();
	 }
	 
	 /**
	  * 我的提问
	  */
	  public function userQuest(){
	  	$uid = session('uid');
		//查询当前提问的内容，包含商品名回复内容和提问内容  要是已回复的才会显示
		$quest = M()->field('g.gid,g.goods_listimg,quest_time,goods_name,quest_content,answer')->join('__questanswer__ qa JOIN __goods__ g ON qa.goods_gid=g.gid')->where("qa.users_uid={$uid} AND qa.answer!=''")->order('qa.quest_time DESC')->all();
		$this->assign('quest',$quest);
		$this->display();
	  }
	 
	 
	 /**
	  * 查询当前用户所有的评论
	  */
	  public function userComms(){
	  	$uid = session('uid');
		
		$comms = M()->field('gc_content,goods_name,gid,gc_time')->join('__goodscomment__ gc JOIN __goods__ g ON gc.goods_gid=g.gid')->where("users_uid={$uid}")->order('gc_time DESC')->all();
		
		$this->assign('comms',$comms);
		$this->display();
	  	
	  }
	 
	 
	 

}
